Package sudoku.Extras

Source Code of sudoku.Extras.Puntaje

/*
* Clase para manejar el puntaje y control de archivos para
* mostrar y guardar el topTen.
*/
package sudoku.Extras;

import java.util.LinkedList;
import java.util.StringTokenizer;
import javax.swing.JOptionPane;
import sudoku.Vista.VistaPuntaje;

/**
*
* @author User
*/

public class Puntaje {
    long tiempoActual; //almacenar tiempo actual.
   
   /*
    * Constructor de la clase puntaje.   
    */
    public Puntaje(){
        tiempoActual = System.currentTimeMillis();
    }
   
   /*
    * calcula el puntaje y comprueba con los demas puntajes del topTen.
    * @param dificultad para saber que topTen es.
    */   
    public void finalizarPuntaje(int dificultad){
        tiempoActual = ((System.currentTimeMillis()) - tiempoActual)/1000 ;
        comprobarPuntaje(dificultad);
    }
   
   /*
    * Comprueba el puntaje obtenido si pertence al topTen o no.
    * @param dificultad para saber que topTen es.
    */
    private void comprobarPuntaje(int dificultad){
        try{
            String nombreArchivo = "TopTen"+dificultad;
            Archivo topTen = new Archivo(nombreArchivo);
           
            //Abre archivo correspondiente a la dificultad.
            LinkedList<String> listaTopTen = topTen.leerArchivo();
          
            String nombre = "";
           
            if (listaTopTen.isEmpty()){
                nombre = obtenerNombre();
                String tiempoString = Long.toString(tiempoActual) + "\t"+nombre+"\t" ;
                listaTopTen.add(tiempoString);
            }else {         
                int indice = 0;
                boolean encontro = false;
                while(indice<listaTopTen.size() && !encontro){
                    StringTokenizer st = new StringTokenizer (listaTopTen.get(indice), "\t");
                    String tiempo = st.nextToken();
                    if (tiempoActual < Integer.parseInt(tiempo)){
                        encontro = true;
                    }
                    indice++;
                }
                nombre = obtenerNombre();
                String tiempoString = Long.toString(tiempoActual) + "\t"+nombre+"\t";
                if (encontro){
                    indice--;
                    listaTopTen.add(indice,tiempoString);
                }else{

                    listaTopTen.add(tiempoString);
                }
               
                if (listaTopTen.size() > 10)
                    listaTopTen.remove(10);
            }
            // Escribe el archivo nuevamente
            topTen.escribirArchivo(listaTopTen);
         
        }
        catch(Exception e){
             e.printStackTrace();
      }
    }

    /*
    * pide al usuario que ingrese su nombre.
    * @return nombre de usuario, anonimo si es blanco o null.
    */   
    private String obtenerNombre(){
        String nombre = "";
        nombre = JOptionPane.showInputDialog(null, "Introduzca su Nombre", "Nuevo Record", JOptionPane.QUESTION_MESSAGE) ;
        if ((nombre == null) || (nombre.isEmpty())){
            nombre = "Anonimo";
        }
        return nombre;
    }
   
    /*
    * Muestra el topTen.
    * @dificultad para leer el archivo correcto
    */
    public static void mostrarPuntaje(int dificultad) {
        try{
            String nombreArchivo = "TopTen"+dificultad;
            Archivo topTen = new Archivo(nombreArchivo);
           
            LinkedList<String> listaTopTen = topTen.leerArchivo();
            VistaPuntaje vistaTopTen = new VistaPuntaje(listaTopTen);
           
        }catch(Exception e){}     
    }
   
}
TOP

Related Classes of sudoku.Extras.Puntaje

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.